package com.cet.test.mt02;

/**
 * @program: cet-practice
 * @description: 字符串转整形
 * @author: 陈恩涛
 * @create: 2023-04-21 17:38
 **/
public class StringToInteger {

    public static void main(String[] args) {
        String input = "134";
        System.out.println(toInteger(input));
    }

    public static Integer toInteger(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        int result = 0;
        int offset = 1;
        int end = 0;
        if (chars[0] == '-') {
            end = 1;
        }
        for (int i = length - 1; i >= end; i--) {
            int temp = chars[i] - '0';
            result += temp * offset;
            offset *= 10;
        }
        return end == 0 ? result : -1 * result;
    }
}
